package com.premize.grco.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.premize.grco.dataaccess.sessionFactory.HibernateSessionFactory;
import com.premize.grco.movil.GrcoPLocalizacion;


/**
 * A data access object (DAO) providing persistence and search support for
 * GrcoPLocalizacion entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class GrcoPLocalizacionDAO implements IGrcoPLocalizacionDAO {
    private static final Log log = LogFactory.getLog(GrcoPLocalizacionDAO.class);

    // property constants
    //public static final Long  CONSLOCALIZACION = "consLocalizacion";
    public static final String CONSLOCALIZACION = "consLocalizacion";

    //public static final String  DESCRIPCION = "descripcion";
    public static final String DESCRIPCION = "descripcion";

    //public static final String  ESTADO = "estado";
    public static final String ESTADO = "estado";

    //public static final Long  NIVEL = "nivel";
    public static final String NIVEL = "nivel";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            GrcoPLocalizacion Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(GrcoPLocalizacion instance) {
        log.debug("saving GrcoPLocalizacion instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            GrcoPLocalizacion Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(GrcoPLocalizacion instance) {
        log.debug("deleting GrcoPLocalizacion instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            GrcoPLocalizacion Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(GrcoPLocalizacion instance) {
        log.debug("updating GrcoPLocalizacion instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public GrcoPLocalizacion findById(Long id) {
        log.debug("finding GrcoPLocalizacion instance with id: " + id);

        try {
            GrcoPLocalizacion instance = (GrcoPLocalizacion) getSession()
                                                                 .get(GrcoPLocalizacion.class,
                    id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding GrcoPLocalizacion failed", re);
            throw re;
        }
    }

    public List<GrcoPLocalizacion> findByExample(GrcoPLocalizacion instance) {
        log.debug("finding GrcoPLocalizacion instance by example");

        try {
            List results = getSession().createCriteria("GrcoPLocalizacion")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  GrcoPLocalizacion entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< GrcoPLocalizacion> found by query
        */
    public List<GrcoPLocalizacion> findByProperty(String propertyName,
        Object value) {
        log.debug("finding GrcoPLocalizacion instance with property: " +
            propertyName + ", value: " + value);

        try {
            String queryString = "from GrcoPLocalizacion as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<GrcoPLocalizacion> findByConsLocalizacion(
        Object consLocalizacion) {
        return findByProperty(CONSLOCALIZACION, consLocalizacion);
    }

    public List<GrcoPLocalizacion> findByDescripcion(Object descripcion) {
        return findByProperty(DESCRIPCION, descripcion);
    }

    public List<GrcoPLocalizacion> findByEstado(Object estado) {
        return findByProperty(ESTADO, estado);
    }

    public List<GrcoPLocalizacion> findByNivel(Object nivel) {
        return findByProperty(NIVEL, nivel);
    }

    /**
    * Find all GrcoPLocalizacion entities.
    *
    * @return List<GrcoPLocalizacion> all GrcoPLocalizacion instances
    */
    public List<GrcoPLocalizacion> findAll() {
        log.debug("finding all GrcoPLocalizacion instances");

        try {
            String queryString = "from GrcoPLocalizacion";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<GrcoPLocalizacion> findByCriteria(String whereCondition) {
        log.debug("finding GrcoPLocalizacion " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from GrcoPLocalizacion model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<GrcoPLocalizacion> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in GrcoPLocalizacion failed", re);
            throw re;
        }
    }

    public List<GrcoPLocalizacion> findPageGrcoPLocalizacion(
        String sortColumnName, boolean sortAscending, int startRow,
        int maxResults) {
        log.debug("finding GrcoPLocalizacion findPageGrcoPLocalizacion");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from GrcoPLocalizacion model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from GrcoPLocalizacion model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberGrcoPLocalizacion() {
        log.debug("finding GrcoPLocalizacion count");

        try {
            String queryString = "select count(*) from GrcoPLocalizacion model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
